package com.cutthroatcreations.woodenrailway;

import java.util.ArrayList;

import com.cutthroatcreations.woodenrailway.SectionedAdapter.SectionedListItem;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class ContentWorker {

	private static final String TAG = "ContentWorker";
	private Context context;
	private Train train;
	private String year;
	private Cursor c;
	private double allOwned;
	private ArrayList<Train> parsed;

	public ContentWorker(Context c) {
		this.context = c;
		this.train = null;
		this.year = null;
	}

	public ContentWorker(Context c, Train t, String y) {
		this.context = c;
		this.train = t;
		this.year = y;
		if (y == "1992/1993")
			this.year = "1993";
	}

	public boolean isOwned() {

		String w = UserProvider.KEY_TRN_ID + " = " + train.getID() + " AND "
				+ UserProvider.KEY_COL_YEAR + " = " + year + " AND "
				+ UserProvider.KEY_USR_ID + " = " + WoodenApplication.user.getId();

		ContentResolver cr = context.getContentResolver();
		Cursor c = cr.query(UserProvider.URI, null, w, null, null);

		if (c.getCount() > 0) {
			c.close();
			return true;
		}
		c.close();
		return false;
	}

	public int checked() {

		String w = UserProvider.KEY_TRN_ID + " = " + train.getID() + " AND "
				+ UserProvider.KEY_COL_YEAR + " = " + year;

		ContentResolver cr = context.getContentResolver();
		c = cr.query(UserProvider.URI, null, w, null, null);

		if (c.getCount() > 0) {
			String where = train.getID() + "." + year;

			int result = cr.delete(UserProvider.URI, where, null);

			c.close();

			return result;

		} else {

			if (c.getCount() == 0) {

				ContentValues values = new ContentValues();
				values.put(UserProvider.KEY_TRN_ID, train.getID());
				values.put(UserProvider.KEY_COL_YEAR, year);
				values.put(UserProvider.KEY_USR_ID,
						WoodenApplication.user.getId());

				cr.insert(UserProvider.URI, values);

				c.close();
				return 0;

			}
		}
		return -1;
	}

	public SectionedListItem[] getCollectionAsItems() {

		ArrayList<SectionedListItem> allItems = new ArrayList<SectionedListItem>();

		ContentResolver cr = context.getContentResolver();
		c = cr.query(UserProvider.URI, null, null, null,
				UserProvider.KEY_COL_YEAR + " ASC");

		if (c.moveToFirst()) {
			do {
				String y = c.getString(c.getColumnIndex("year"));
				if (y == "1993")
					y = "1992/1993";
				allItems.add(new SectionedListItem(WoodenApplication
						.getTrainById(c.getInt(c.getColumnIndex("train_id"))),
						y));
			} while (c.moveToNext());
		}
		SectionedListItem[] allItemsOwned = allItems
				.toArray(new SectionedListItem[allItems.size()]);
		return allItemsOwned;
	}

	public ArrayList<Owned> getCollectionForExport() {

		ArrayList<Owned> allItems = new ArrayList<Owned>();

		ContentResolver cr = context.getContentResolver();
		c = cr.query(UserProvider.URI, null, null, null,
				UserProvider.KEY_COL_YEAR + " ASC");

		if (c.moveToFirst()) {
			do {
				allItems.add(new Owned(
						c.getString(c.getColumnIndex("train_id")), c
								.getString(c.getColumnIndex("year"))));
			} while (c.moveToNext());
		}
		return allItems;
	}

	public void allOwned() {

		this.allOwned = 0;

		ContentResolver cr = context.getContentResolver();
		c = cr.query(UserProvider.URI, null, null, null, null);

		int totalItemsAdded = 0;

		for (Train train : WoodenApplication.allTrains) {
			totalItemsAdded = train.getNumYears() + totalItemsAdded;
		}

		double numCol = (double) c.getCount();
		double totalItems = (double) totalItemsAdded;

		this.allOwned = (numCol / totalItems) * 100;

		c.close();

	}

	public void yearOwned(int index) {

		this.allOwned = 0;

		String y = WoodenApplication.cYears[index];

		if (y == "All Items")
			return;
		if (y == "1992/1993")
			y = "1993";

		String where = UserProvider.KEY_COL_YEAR + " = " + y;
		parsed = new ArrayList<Train>();
		for (Train train : WoodenApplication.allTrains) {

			for (Integer i : train.allYears) {

				if (i == Integer.parseInt(y)) {

					parsed.add(train);

				}
			}
		}

		ContentResolver cr = context.getContentResolver();

		c = cr.query(UserProvider.URI, null, where, null, null);

		double numCol = (double) c.getCount();
		double totalItems = (double) parsed.size();

		this.allOwned = (numCol / totalItems) * 100;

		c.close();
	}

	public double getOwned() {
		return allOwned;
	}

}
